package com.realphim.app.adapter;

import java.util.List;

import com.realphim.app.ActivityCategory;
import com.realphim.app.ActivityMain;
import com.realphim.app.R;
import com.realphim.app.component.LayoutCategory;
import com.realphim.app.model.ModelChild;
import com.realphim.app.model.ModelGroup;
import com.realphim.app.util.CommonDefine;
import com.realphim.app.util.CommonDefine.KeyBundle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AdapterLeftMenu extends BaseExpandableListAdapter implements
        CommonDefine {

    private List<ModelGroup> listGroup;

    private LayoutInflater inflater = null;

    private Context context;

    public AdapterLeftMenu(Context context, List<ModelGroup> listGroup) {
        this.listGroup = listGroup;
        this.inflater = ((Activity) context).getLayoutInflater();
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return listGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return listGroup.get(groupPosition).listChild.size();
    }

    @Override
    public ModelGroup getGroup(int groupPosition) {
        return listGroup.get(groupPosition);
    }

    @Override
    public ModelChild getChild(int groupPosition, int childPosition) {
        return listGroup.get(groupPosition).listChild.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    private class GroupHolder {
        TextView tvTitle;

        ImageView ivGroup;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        ModelGroup mGroup = getGroup(groupPosition);
        View vi = convertView;
        if (vi == null) {
            vi = inflater.inflate(R.layout.row_menu_group, null);
            GroupHolder holder = new GroupHolder();
            holder.tvTitle = (TextView) vi.findViewById(R.id.tvMenuTitle);
            holder.ivGroup = (ImageView) vi.findViewById(R.id.ivGroup);
            vi.setTag(holder);
        }

        GroupHolder holder = (GroupHolder) vi.getTag();
        holder.tvTitle.setText(mGroup.title);
        if (isExpanded) {
            holder.ivGroup.setImageResource(R.drawable.minus);
        } else {
            holder.ivGroup.setImageResource(R.drawable.plus);
        }
        return vi;
    }

    private class ChildHolder {
        RelativeLayout rlChild;

        TextView tvTitle;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        ModelChild mChild = getChild(groupPosition, childPosition);
        View vi = convertView;
        if (vi == null) {
            vi = inflater.inflate(R.layout.row_menu_child, null);
            ChildHolder holder = new ChildHolder();
            holder.tvTitle = (TextView) vi.findViewById(R.id.tvChildTitle);
            holder.rlChild = (RelativeLayout) vi.findViewById(R.id.rlChild);
            vi.setTag(holder);
        }
        ChildHolder holder = (ChildHolder) vi.getTag();
        holder.tvTitle.setText(mChild.title);
        holder.rlChild.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, ActivityCategory.class);
                i.putExtra(
                        KeyBundle.CATEGORY_NAME,
                        getGroup(groupPosition).title + " - "
                                + getChild(groupPosition, childPosition).title);
                i.putExtra(
                        KeyBundle.URL_GET_PHIM,
                        SERVICE_GET_LIST_FILM + ServiceParam.TYPE + "="
                                + getGroup(groupPosition).type + "&"
                                + ServiceParam.CATEGORY_ID + "="
                                + getChild(groupPosition, childPosition).id);
                context.startActivity(i);
                ((Activity) context).overridePendingTransition(
                        R.anim.slide_in_left, R.anim.slide_out_left);
            }
        });
        return vi;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}
